﻿using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Magellan.Routing
{
    public class PathMatch
    {
        private readonly bool _success;
        private readonly IRoute _route;
        private readonly List<object> _segmentValues;
        private readonly string _failReason;

        protected PathMatch(bool success, IRoute route, List<object> segmentValues, string failReason)
        {
            _success = success;
            _route = route;
            _segmentValues = segmentValues;
            _failReason = failReason;
        }

        public static PathMatch Successful(IRoute route, List<object> segmentValues)
        {
            return new PathMatch(true, route, segmentValues, null);
        }

        public static PathMatch Failure(IRoute route, string reason)
        {
            return new PathMatch(false, null, null, reason);
        }

        public bool Success
        {
            get { return _success; }
        }

        public IRoute Route
        {
            get { return _route; }
        }

        public IList<object> SegmentValues
        {
            get { return _segmentValues; }
        }

        public string Path
        {
            get 
            {
                return string.Join("/", 
                    _segmentValues
                    .Where(x => x != null)
                    .Where(x => x is string == false || (x is string && ((string)x).Length > 0))
                    .ToArray()); 
            }
        }

        public string FailReason
        {
            get { return _failReason; }
        }
    }
}